package mk.com.ldm.corepoint.administration.client.lib.CustomPagingScrollTable;


import org.gwt.mosaic.ui.client.table.FixedWidthFlexTable;
import org.gwt.mosaic.ui.client.table.FixedWidthGrid;
import org.gwt.mosaic.ui.client.table.ScrollTable;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class CustomPagingScrollTable extends ScrollTable implements HasHandlers{
	private int pageSize = 50;
	private int currentPage = 1;
	private int totalPages = 1;
	private Button next = new Button(">");
	private Button prev = new Button("<");
	private Button last = new Button(">>");
	private Button first = new Button("<<");
	private ScrollTable table = this;
	private HandlerManager handlerManager;
	private PageChangedEvent pageChangedEvent = new PageChangedEvent();
	private TextBox currPageTxt, pageSizeTxt;
	
	public CustomPagingScrollTable(FixedWidthGrid dataTable, FixedWidthFlexTable headerTable) {
		super(dataTable, headerTable);
		init();
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	
	public HandlerRegistration addPageChangedEventHandler(PageChangedEventHandler handler){
		return handlerManager.addHandler(PageChangedEvent.TYPE, handler);
	}
	
	public void fireEvent(GwtEvent<?> event){
		handlerManager.fireEvent(event);
	}
	
	private void init(){
		handlerManager = new HandlerManager(this);
		
		FixedWidthFlexTable footer = new FixedWidthFlexTable();
		
		HorizontalPanel pageInfo = new HorizontalPanel();
		currPageTxt = new TextBox();
		currPageTxt.setWidth("40px");
		currPageTxt.setText(Integer.toString(currentPage));
		Label totalPagesLbl = new Label();
		totalPagesLbl.setText("/"+totalPages);
		pageInfo.add(currPageTxt);
		pageInfo.add(totalPagesLbl);
		
		pageSizeTxt = new TextBox();
		pageSizeTxt.setText(""+pageSize);
		pageSizeTxt.setWidth("60px");
		
		footer.setWidget(0, 0, first);
		footer.setWidget(0, 1, prev);
		footer.setWidget(0, 2, pageInfo);
		footer.setWidget(0, 3, next);
		footer.setWidget(0, 4, last);
		footer.setWidget(0, 5, pageSizeTxt);
		
		table.setFooterTable(footer);
		
		next.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				currentPage += 1;
				if(currentPage>totalPages){
					currentPage = totalPages;
				}
				doChange();
			}
		});
		prev.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				currentPage -= 1;
				if(currentPage<1){
					currentPage = 1;
				}
				doChange();
			}
		});
		last.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				currentPage = totalPages;
				doChange();
			}
		});
		first.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				currentPage = 1;
				doChange();
			}
		});
		currPageTxt.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				try{
					int value = Integer.parseInt(currPageTxt.getText());
					if(value<=totalPages&&value>0){
						currentPage = value;
						doChange();
					}
				}
				catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
		pageSizeTxt.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				try{
					int value = Integer.parseInt(pageSizeTxt.getText());
					if(value>0){
						pageSize = value;
						doChange();
					}
				}
				catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
	}
	
	private void doChange(){
		pageChangedEvent.setCurrentPage(currentPage);
		pageChangedEvent.setPageSize(pageSize);
		fireEvent(pageChangedEvent);
		currPageTxt.setText(Integer.toString(currentPage));
		table.redraw();
	}
}
